import 'dart:convert';

import 'package:any_reader/public.dart';

import '../provider/export.dart';
import 'export.dart';

class BookSheetModel extends ViewStateModel {
  ThemeModel themeModel;
  BookSheetModel(this.themeModel);
  List<Book> list = [];
  List<Book> recommendList = [];
  List<Book> hotList = [];
  initData() async {
    FlutterStatusbarManager.setColor(themeModel.customThemeData.mainColor);
    list = (await DatabaseHelper().getAllBook());
    notifyListeners();
    recommendList = (await BookService.getRecommend(limit: 10));
    hotList = (await BookService.getRecommend(limit: 10));
    notifyListeners();

    EventBus().on("bookSheetRefresh", (arg) {
      this.refreshLocal();
    });
  }

  Future<void> deleteBook(String bookId) async {
    await DatabaseHelper().deleteBook(bookId);
    this.initData();
  }

  Future<void> refreshLocal() async {
    list = (await DatabaseHelper().getAllBook());
    notifyListeners();
  }

  Future<void> refresh() async {
    recommendList = (await BookService.getRecommend(limit: 10));
    hotList = (await BookService.getRecommend(limit: 10));
    notifyListeners();
  }
}
